/**
 * @description: 二次封装原生的弹框
 */

/** 确定按钮的颜色 */
const confirmColor = '#1177ff'

/** toast提示 */
export function toast(title: string | any, duration = 2500, mask = false): Promise<number> {
  return new Promise((resolve) => {
    if (title !== undefined) {
      wx.showToast({
        title,
        duration,
        mask,
        icon: 'none',
      })
    }
    setTimeout(resolve, duration)
  })
}

/** loading提示 */
export function loading(title: string = '加载中...', mask = true): Promise<boolean> {
  return new Promise((resolve) => {
    wx.showLoading({
      title,
      mask,
      success() {
        resolve(true)
      },
      fail() {
        resolve(false)
      },
    })
  })
}

/** 关闭loading提示 */
export function hideLoading() {
  wx.hideLoading()
}

/** alert提示 */
export function alert(options: IAlert | string): Promise<unknown> {
  let newOptions = typeof options === 'string' ? { content: options } : options
  return new Promise((resolve, reject) => {
    wx.showModal({
      title: '温馨提示',
      showCancel: false,
      confirmColor,
      ...newOptions,
      success: function (res) {
        if (res.confirm) {
          resolve(true)
        } else if (res.cancel) {
          reject(false)
        }
      },
      fail: function (err) {
        reject(err)
      },
    })
  })
}

/** confirm提示 */
export function confirm(options: IConfirm | string): Promise<unknown> {
  let newOptions = typeof options === 'string' ? { content: options } : options
  return new Promise((resolve, reject) => {
    wx.showModal({
      title: '温馨提示',
      showCancel: true,
      confirmColor,
      ...newOptions,
      success: function (res) {
        if (res.confirm) {
          resolve(true)
        } else if (res.cancel) {
          reject(false)
        }
      },
      fail: function (err) {
        reject(err)
      },
    })
  })
}